package br.com.onlinebidding.mb;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import br.com.onlinebidding.facade.UserFacade;

import br.com.onlinebidding.model.User;

@RequestScoped
@ManagedBean
public class LoginMB extends AbstractMB {
	@ManagedProperty(value = UserMB.INJECTION_NAME)
	private UserMB userMB;
	
	private String id;
	private String email;
	private String password;
	
	public String getId() {return id;}
	public void setId(String id) {this.id = id;}

	public String getEmail() {return email;}
	public void setEmail(String email) {this.email = email;}

	public String getPassword() {return password;}
	public void setPassword(String password) {this.password = password;}

	
	
	public String login() {

		UserFacade userFacade = new UserFacade();
		User user = userFacade.isValidLogin(email, password);

		if (user != null) {
			userMB.setUser(user);
			FacesContext context = FacesContext.getCurrentInstance();
			HttpServletRequest request = (HttpServletRequest) context
					.getExternalContext().getRequest();
			request.getSession().setAttribute("user", user);
			userMB.setUserLogged(user);

			return "/view/protected/index.xhtml";

		}
		displayErrorMessageTo("Verifique Nome/Senha");
		return null;
	}

	public void setUserMB(UserMB userMB) {this.userMB = userMB;}
}